package com.enbatis.mybatisplugs.controller;

import com.enbatis.mybatisplugs.base.BaseController;
import com.enbatis.mybatisplugs.commons.sql.Wrapper;
import com.enbatis.mybatisplugs.commons.utils.ResultReturn;
import com.enbatis.mybatisplugs.entity.TProduct;
import com.enbatis.mybatisplugs.entity.vo.TPurchaseCartVO;
import com.enbatis.mybatisplugs.plugin.Pages;
import com.github.pagehelper.Page;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.enbatis.mybatisplugs.entity.TPurchaseCart;
import com.enbatis.mybatisplugs.service.TPurchaseCartService;


import java.util.List;

@RestController
@RequestMapping("v1/t_purchase_cart")
public class TPurchaseCartController extends BaseController {

@Autowired
private TPurchaseCartService tPurchaseCartService;


    @PostMapping("/add")
    public ResultReturn<TPurchaseCart> add(@RequestBody TPurchaseCart tPurchaseCart){
        return  success(tPurchaseCartService.insert(tPurchaseCart));
    }


    @ApiOperation(value = "添加到采购车", notes = "添加到采购车")
    @PostMapping("/add_to_cart")
    public ResultReturn<Boolean> addToCart(@RequestBody TPurchaseCart tPurchaseCart){
        tPurchaseCart.setCompanyId(getAccount().getCompanyId());
        tPurchaseCart.setUid(getAccount().getId());
        tPurchaseCart.setName(getAccount().getUsername());
        return  success(tPurchaseCartService.addToCart(tPurchaseCart));
    }


    @DeleteMapping("/delete")
    public ResultReturn<Integer> delete(@RequestParam("id") Long id){
        return success(tPurchaseCartService.deleteById(id));
    }


    @PutMapping("/{id}")
    public  ResultReturn<Integer> update(@PathVariable("id") Long id,@RequestBody TPurchaseCart tPurchaseCart){
        tPurchaseCart.setId(id);
        return success(tPurchaseCartService.updateById(tPurchaseCart));
    }


    @GetMapping("/{id}")
    public ResultReturn<TPurchaseCart> get(@PathVariable("id") Long id) {
        return success(tPurchaseCartService.getById(id));
    }


    @GetMapping("/list")
    public ResultReturn<List<TPurchaseCart>> getList(TPurchaseCart tPurchaseCart){
        return success(tPurchaseCartService.list(new Wrapper<>(tPurchaseCart)));
    }


    @ApiOperation(value = "采购车查询分页列表", notes = "分页查询数据列表")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "请求数据的页码", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "rows", value = "每页条数", required = true, paramType = "query", dataType = "int"),
            @ApiImplicitParam(name = "sidx", value = "排序字段", paramType = "query", dataType = "String"),
            @ApiImplicitParam(name = "sord", value = "排序方式", paramType = "query", dataType = "String"),
    })
    @PostMapping("/page_vo")
    public ResultReturn<Pages<TPurchaseCartVO>> getPage(@RequestBody TPurchaseCartVO vo){
        Page<TPurchaseCartVO> page= getPage();
        return success(tPurchaseCartService.pageVO(page,vo,getAccount()));
    }

}
